import { createSelector } from "@ngrx/store";


import { MapState, DrawToolState, DrawTool } from "../reducers";
import { getMapState } from "./map.selector";
import { DrawToolTypes } from "../actions/draw-tool.action";

const interactionDrawType: string[] = [
    DrawToolTypes.POINT,
    DrawToolTypes.LINESTRING,
    DrawToolTypes.CIRCLE,
    DrawToolTypes.RECTANGLE,
    DrawToolTypes.POLYGON
]


export const getDrawToolState = createSelector(getMapState, (state: MapState) => state.drawTool);

export const getDrawToolEntities = createSelector(getDrawToolState, (state: DrawToolState) => state.entities);

export const getDrawToolSelect = createSelector(getDrawToolEntities, (entities: { [key: string]: DrawTool }) => entities[DrawToolTypes.SELECT]);
export const getDrawToolPoint = createSelector(getDrawToolEntities, (entities: { [key: string]: DrawTool }) => entities[DrawToolTypes.POINT]);
export const getDrawToolLineString = createSelector(getDrawToolEntities, (entities: { [key: string]: DrawTool }) => entities[DrawToolTypes.LINESTRING]);
export const getDrawToolCircle = createSelector(getDrawToolEntities, (entities: { [key: string]: DrawTool }) => entities[DrawToolTypes.CIRCLE]);
export const getDrawToolRectangle = createSelector(getDrawToolEntities, (entities: { [key: string]: DrawTool }) => entities[DrawToolTypes.RECTANGLE]);
export const getDrawToolPolygon = createSelector(getDrawToolEntities, (entities: { [key: string]: DrawTool }) => entities[DrawToolTypes.POLYGON]);
export const getDrawToolInputMark = createSelector(getDrawToolEntities, (entities: { [key: string]: DrawTool }) => entities[DrawToolTypes.INPUTMARK]);
export const getDrawToolBuffer = createSelector(getDrawToolEntities, (entities: { [key: string]: DrawTool }) => entities[DrawToolTypes.BUFFER]);
export const getDrawToolRemove = createSelector(getDrawToolEntities, (entities: { [key: string]: DrawTool }) => entities[DrawToolTypes.REMOVE]);
export const getDrawToolClean = createSelector(getDrawToolEntities, (entities: { [key: string]: DrawTool }) => entities[DrawToolTypes.CLEAN]);

export const getDrawToolMeasureAvailable = createSelector(getDrawToolState,  (state: DrawToolState) => state.measureAvailable);

export const getInteractionDrawType = createSelector(getDrawToolEntities, (entities: { [key: string]: DrawTool }) => {
    return Object.keys(entities).find(key => {
        return interactionDrawType.includes(key) && entities[key].active;
    });
});

export const getDrawToolBufferOptions = createSelector(getDrawToolState, (state: DrawToolState) => state.buffer);

export const getDrawToolInputMarkOptions = createSelector(getDrawToolState, (state: DrawToolState) => state.inputMark);


